package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 丹东信息港
 * @author Persh
 *
 */
public class _041599NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.041599.com/html/[0-9]{6}/[0-9]*/[0-9]*.htm"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("font.bt_16").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("td.txt_28"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("span.nava1"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#articlecontent"));
		}
		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来自：");
		if(StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "来源:");
		}
		date=DateUtils.matchDate(doc.select("td[align=middle]").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.ny_list01").text());
		}
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		_041599NewsAnalyse analyse=new _041599NewsAnalyse();
		String url="http://www.041599.com/html/201506/22/085225914.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
